﻿using System.Windows;
using System.Windows.Controls;
using Red.Controls;
using Red.Controls.Shell;
using Red.Controls.Utils;
using Dialog = Red.Launcher.Utilities.Dialog;
using ViewModel = Red.Launcher.ViewModels.ExternalApplications;

namespace Red.Launcher.Menus.ExternalApplications
{
    public partial class ApplicationConfigurationDialog : RedWindow, Dialog.IEvents
    {
        #region Public Properties

        public ViewModel.Application Application
        {
            get { return DataContext as ViewModel.Application; }
            set { DataContext = value; }
        }

        public bool IsValid
        {
            get
            {
                bool isValid = true;

                isValid &= Helpers.ValidateElement( _name );
                isValid &= Helpers.ValidateElement( _path );

                return isValid;
            }
        }

        #endregion Public Properties

        #region C-Tor

        public ApplicationConfigurationDialog()
        {
            InitializeComponent();
        }

        #endregion C-Tor

        #region Public Methods

        public void RefreshBindingsForValidation()
        {
            Helpers.RefreshBindingValues( _name, TextBox.TextProperty );
            Helpers.RefreshBindingValues( _path, ButtonTextbox.TextProperty );
        }

        #endregion Public Methods

        #region Event Handlers

        private void OK_Click( object sender, RoutedEventArgs e )
        {
            RefreshBindingsForValidation();

            if ( IsValid )
            {
                Ok?.Invoke();

                Close();
            }
        }

        private void Cancel_Click( object sender, RoutedEventArgs e )
        {
            Close();
        }

        #endregion Event Handlers

        #region IEvents

        public bool Propagated { get; set; }
        public event Dialog.InterfaceClosed Ok;

        #endregion IEvents
    }
}
